package com.onlineMIS.ORM.DAO.headQ.barCodeGentor;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.onlineMIS.ORM.DAO.BaseDAO;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Category;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.ProductBarcode;
import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.loggerLocal;
import com.onlineMIS.filter.SystemParm;

@Repository
public class CategoryDaoImpl extends BaseDAO<Category> {
	
	public static Category getDefaultCategoryLevel1() {
		Category category = new Category();
		category.setCategory_ID(Category.CATEGORY_LEVEL1_DEFAUL);
		
		return category;
	}
	
	public List<Category> getHeadCategryLevel2(boolean cached, int status){
		DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
		criteria.add(Restrictions.eq("chainId", Category.TYPE_HEAD));
		criteria.add(Restrictions.eq("status", Integer.valueOf(status)));
		criteria.add(Restrictions.eq("level", Category.LEVEL2));
		return this.getByCritera(criteria, cached);
	}
	
	/**
	 * 根据货品级别和状态查询货品信息
	 * @param cached
	 * @param status
	 * @return
	 */
	public List<Category> getHeadCategryByLevel(boolean cached,int categoryLevel, int status) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
		criteria.add(Restrictions.eq("chainId", Category.TYPE_HEAD));
		criteria.add(Restrictions.eq("status", Integer.valueOf(status)));
		criteria.add(Restrictions.eq("level", categoryLevel));
		return this.getByCritera(criteria, cached);
	}
	

	public Category getCategoryLevel2ByName(String categoryString) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
		criteria.add(Restrictions.eq("category_Name", categoryString));
		criteria.add(Restrictions.eq("chainId", Category.TYPE_HEAD));
		criteria.add(Restrictions.eq("status", Category.STATUS_GOOD));
		List<Category> categories = this.getByCritera(criteria, true);
		
		return categories.get(0);
	}
	
	public List<Category> getChainCategry(boolean cached){
		DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
		criteria.add(Restrictions.eq("chainId", Category.TYPE_CHAIN));
		criteria.add(Restrictions.eq("level", Category.LEVEL2));
		return this.getByCritera(criteria, cached);
	}
	
	/**
	 * 校验是否是鞋子，饰品，玩具，婴装，朴与素
	 * @param category
	 * @return
	 */
	public boolean isCheckChainCategry(String category){
		DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
		criteria.add(Restrictions.eq("category_Name", category));
		criteria.add(Restrictions.eq("chainId", Category.TYPE_CHAIN));
		criteria.add(Restrictions.eq("level", Category.LEVEL2));
		List<Category> categories = this.getByCritera(criteria, true);
		if (categories == null || categories.size() == 0)
			return true;
		else 
			return false;
	}
	
	/**
	 * 查询店铺货品类别
	 * @param categoryString
	 * @return
	 */
	public Category getChainCategoryName(String categoryString) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
		criteria.add(Restrictions.eq("category_Name", categoryString));
		criteria.add(Restrictions.eq("chainId", Category.TYPE_CHAIN));
		criteria.add(Restrictions.eq("level", Category.LEVEL2));
		List<Category> categories = this.getByCritera(criteria, false);		
		return categories.get(0);
	}

	

	public boolean checkCategoryExist(String category, int status){
		DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
		criteria.add(Restrictions.eq("category_Name", category));
		criteria.add(Restrictions.eq("chainId", Category.TYPE_HEAD));
		criteria.add(Restrictions.eq("status", status));
		List<Category> categories = this.getByCritera(criteria, true);
		
		if (categories == null || categories.size() != 1)
			return false;
		else 
			return true;
	}

	

	public List<Category> getAllLevel1(boolean cached) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
		criteria.add(Restrictions.eq("level", Category.LEVEL1));
		criteria.add(Restrictions.eq("status", Category.STATUS_GOOD));
		return this.getByCritera(criteria, cached);
	}

	/**
	 * 一级货品类产品线根据货品类id查询货品类信息
	 * @param categoryLevelId
	 * @param cached
	 * @return
	 */
	public Category getCategoryLevel1ById(int categoryLevelId, boolean cached) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
		criteria.add(Restrictions.eq("level", Category.LEVEL1));
		criteria.add(Restrictions.eq("status", Category.STATUS_GOOD));
		criteria.add(Restrictions.eq("category_ID",categoryLevelId));
		List<Category> categoryLevel1 =  this.getByCritera(criteria, cached);
		if (categoryLevel1==null || categoryLevel1.size() == 0)
			return null;
		else 
			return categoryLevel1.get(0);
	}

	


	public Category selectByCategoryName(String categorySuffix) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
		criteria.add(Restrictions.eq("category_Name", categorySuffix));	
		criteria.add(Restrictions.eq("status", Category.STATUS_GOOD));
		List<Category> list =  this.getByCritera(criteria, true);
		return list.get(0);
	}

	
	
	/**
	 * 根据productBarcode判断是否是千禧物品或者禧乐仓物品
	 * @param productBarcode
	 * @return
	 */
	public Boolean isQxOrXlecItem(ProductBarcode productBarcode) {
		String category =  "";
		String categoryParam = SystemParm.getQXCategory();
		if (productBarcode != null) {
			category = Integer.toString(productBarcode.getProduct().getCategory().getCategory_ID());
			if (!category.contains(categoryParam)) {
				return true;
			}else {
				return false;
			}
		}				
		return false;
	}

	/**
	 * 获取level2的集合信息
	 * @return
	 */
	public List<Category> selectLecelOneCounts() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
		criteria.add(Restrictions.eq("level", Category.LEVEL2));		
		criteria.add(Restrictions.eq("status", Category.STATUS_GOOD));
		List<Category> list =  this.getByCritera(criteria, false);
		return list;
	}

	/**
	 * 获取level2 已经赋值编码的数量
	 * @return
	 */
	public int selectIsCodeNum() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
		criteria.add(Restrictions.eq("level", Category.LEVEL2));	
	
		criteria.setProjection(Projections.rowCount());
		
		return Common_util.getProjectionSingleValue(this.getByCriteriaProjection(criteria, false));
	}

	/**
	 * 获取货品类的最大编号
	 * @return
	 */
	public String selectMaxCode() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
		criteria.add(Restrictions.eq("level", Category.LEVEL2));	
	
		criteria.setProjection(Projections.max("category_Code"));
		int maxCode = Common_util.getProjectionSingleValue(this.getByCriteriaProjection(criteria, false)) + 1;
		return Integer.toString(maxCode);		
	}
	
	
}
